﻿Imports System.Data.SqlClient
Imports System.IO

Public Class adduser

    Public MainForm As Form1
    Private Sub adduser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DocumentManagementDataSet.dept' table. You can move, or remove it, as needed.
        Me.DeptTableAdapter.Fill(Me.DocumentManagementDataSet.dept)
        'TODO: This line of code loads data into the 'DocumentManagementDataSet.company' table. You can move, or remove it, as needed.
        Me.UsersTableAdapter.Fill(Me.DocumentManagementDataSet.users)
        Me.CompanyTableAdapter.Fill(Me.DocumentManagementDataSet.company)

    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        lblerror.Text = ""
        Dim con As New SqlConnection

        con.ConnectionString = MainForm.settingsdialog.txtconstr.Text
        Dim sql As String = ""
        If Button1.Text = "Add" Then
            sql = "INSERT INTO [DocumentManagement].[dbo].[users] VALUES ('" & txtuser.Text.ToLower & "','" & txtpassword.Text.ToLower & "','" & txtemail.Text.ToLower & "','" & selectcompany.SelectedValue & "','" & selectdept.SelectedValue & "','" & selectrole.Text & "')"
        ElseIf Button1.Text = "Edit" Then
            sql = "UPDATE [DocumentManagement].[dbo].[users] SET [email] ='" & txtemail.Text & "', [password]='" & txtpassword.Text.ToLower & "',[company]='" & selectcompany.SelectedValue & "', [department]='" & selectdept.SelectedValue & "',[role]='" & selectrole.Text & "' WHERE [username] ='" & txtuser.Text & "';"
            ' MsgBox(sql)
        End If

        Dim cmd As New SqlCommand(sql, con)
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            lblerror.Text = "Success"
            DataGridView1.Refresh()
            Me.UsersTableAdapter.Fill(Me.DocumentManagementDataSet.users)
            DataGridView1.Refresh()

            reset()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        con.Close()
    End Sub
    Dim rowClicked As Integer
    Private Sub DataGridView1_MouseClick(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
            ContextMenuStrip1.Show(DataGridView1, e.Location)
        End If
    End Sub

    Private Sub EditToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EditToolStripMenuItem.Click
        Try
            txtuser.ReadOnly = True
            txtuser.Text = DataGridView1.SelectedRows(0).Cells(0).Value
            txtpassword.Text = DataGridView1.SelectedRows(0).Cells(1).Value
            txtemail.Text = DataGridView1.SelectedRows(0).Cells(2).Value
            selectcompany.Text = DataGridView1.SelectedRows(0).Cells(3).Value
            selectdept.Text = DataGridView1.SelectedRows(0).Cells(4).Value
            selectrole.Text = DataGridView1.SelectedRows(0).Cells(5).Value
            Button1.Text = "Edit"
        Catch ex As Exception

        End Try

    End Sub
    Sub reset()
        txtuser.Text = ""
        txtpassword.Text = ""
        txtemail.Text = ""
        selectcompany.Text = DataGridView1.SelectedRows(0).Cells(3).Value
        selectdept.Text = DataGridView1.SelectedRows(0).Cells(4).Value
        selectrole.Text = DataGridView1.SelectedRows(0).Cells(5).Value
        txtuser.ReadOnly = False
        Button1.Text = "Add"
        txtuser.Focus()
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        reset()
    End Sub

    
End Class